'''
Created on 27-Sep-2010

@author: dexen
'''


import os
import sys
    
class Ind(object):
    def __init__(self):
        self.birth_time = None
        self.death_time = None
        self.score_cost = None
        self.score_daylight = None
        self.score_energy = None
    
    def output(self):
        print self.birth_time, self.death_time, self.score_cost, self.score_daylight, self.score_energy

def readToInds():
    inds = {}
    top = sys.argv[1]
    print "top", top
    print os.listdir(top)
    for interval in os.listdir(top):
        curdir = os.path.join(top, interval)
        print "curdir", curdir
        for id in os.listdir(curdir):
            #print "id", id
            iddir = os.path.join(curdir, id)
            ind = Ind()
            for name in os.listdir(iddir):
                if name.endswith(".txt") and name != "genotype.txt":
                    f = open(os.path.join(iddir, name), "r")
                    x = f.read()
                    f.close()
                    setattr(ind, name[:-4], float(x))
            inds[int(id)] = ind    


    #for i in range(1,5):
    #    inds[i].output()
    return inds

def main():
    global inds
    inds = readToInds()
    print len(inds.keys())
    f = open("pareto.csv", "w")
    f.write("id,daylight,energy,cost\n")
    
    for id, ind in inds.items():
        if ind.score_cost and ind.score_daylight and ind.score_energy:
            f.write(str(id) + "," + str(round(ind.score_daylight,2)) + "," + \
                    str(round(ind.score_energy,2)) + "," + str(round(ind.score_cost,2)) + "\n")
            
    f.close()


if __name__ == "__main__":
    main()
    